using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

using IGlobalist.Net;
using IGlobalist.Net.RTP;

namespace Experiment
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        RTPSession ses;
        RTPSession ses1;
        private void button1_Click(object sender, EventArgs e)
        {
            RTPSourceDescription sdesc = new RTPSourceDescription("anhdv");
            Socket rtpSock = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP);
            IPEndPoint ep1 = new IPEndPoint(IPAddress.Parse("192.168.1.10"), 12002);
            rtpSock.Bind(ep1);
            Socket rtcpSock = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP);
            IPEndPoint ep2 = new IPEndPoint(IPAddress.Parse("192.168.1.10"), 12003);
            rtcpSock.Bind(ep2);

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.10"),12000);
            ses = new RTPSession(sdesc, rtcpSock, rtpSock,ep);
            ses.ParticipantAdded += new RTP_ParticipantEventHandler(ses_ParticipantAdded);
            ses.PacketReceived +=new PacketReceivedEventHandler(ses_PacketReceived);
            //ses.SendStreams.ToArray()[0].
            ses.Start();            
            //IPEndPoint partEp = new IPEndPoint(IPAddress.Parse("192.168.1.10"),12004);
            //RTPParticipant part1 = new RTPParticipant(partEp);            
           
        }
        private delegate void UpdateGui(string data);
        void ses_PacketReceived(RTPPacketEventArgs e)
        {
            byte[] data = (byte[]) e.Packet.Payload;
            string sdata = System.Text.Encoding.ASCII.GetString(data);
            this.BeginInvoke(new UpdateGui(UpdateData), new object[] {sdata });
        }
        private void UpdateData(string data)
        {
            textBox1.Text += data + "\n";
        }
        void ses_ParticipantAdded(RTP_ParticipantEventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ses.Stop();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            RTPSourceDescription sdesc = new RTPSourceDescription("anhdv1");
            Socket rtpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP);
            IPEndPoint ep1 = new IPEndPoint(IPAddress.Parse("192.168.1.10"), 12000);
            rtpSock.Bind(ep1);
            Socket rtcpSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.IP);
            IPEndPoint ep2 = new IPEndPoint(IPAddress.Parse("192.168.1.10"), 12001);
            rtcpSock.Bind(ep2);

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.1.10"), 12002);
            ses1 = new RTPSession(sdesc, rtcpSock, rtpSock, ep);
            ses1.ParticipantAdded += new RTP_ParticipantEventHandler(ses1_ParticipantAdded);
            ses1.PacketReceived += new PacketReceivedEventHandler(ses1_PacketReceived);
            ses1.Start();            
            
        }

        void ses1_PacketReceived(RTPPacketEventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        void ses1_ParticipantAdded(RTP_ParticipantEventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        private int _ts = 0;
        private void button4_Click(object sender, EventArgs e)
        {
            RTPSendStream sStream = ses1.RtpSenderStream;
            sStream.Send(true, _ts++, System.Text.Encoding.ASCII.GetBytes("Hello RTP"));
        }
    }
}